/**   
* @{#} AlgmSelector.java Create on 2013-8-5 下午4:26:23   
*   
* Copyright (c) 2012 by Baidu.   
*/
package objtable.algm;

import java.util.HashMap;
import java.util.List;

import objtable.algm.extend.HashJoinBy;
import objtable.algm.extend.HashMatchBy;
import objtable.algm.extend.IndexedFilterBy;
import objtable.struct.Row;

/**   
* @author <a href="mailto:zhangbo07@baidu.com">zhangbo07</a>  
* @version 1.0   
*/

public class AlgmSelector {

	private static HashMap<String, IndexedFilterBy> hm_filter = new HashMap<String, IndexedFilterBy>();

	public static Algm1Row getFilterBy(String express, List<Row> rows) {
		boolean canIndex = checkIndex(express);

		if (canIndex) {
			IndexedFilterBy ifb = new IndexedFilterBy(express, rows);
			if (hm_filter.get(ifb.getCacheKey()) == null) {
				hm_filter.put(ifb.getCacheKey(), ifb);
			}
			ifb = hm_filter.get(ifb.getCacheKey());
			//设置新的表达式，这块有些小饶
			ifb.setNewExpress(express);
			return ifb;
		} else {
			return new FilterBy(express);
		}
	}

	private static boolean checkIndex(String express) {
		if (express.indexOf("||") > 0 || express.indexOf(">") > 0
				|| express.indexOf("<") > 0) {
			return false;
		} else {
			return true;
		}
	}

	public static Algm2Row getMatchBy(String simpleExpress,
			String complexExpress) {
		boolean canIndex = checkIndex(simpleExpress);

		if (canIndex) {
			HashMatchBy hmb = new HashMatchBy(simpleExpress, complexExpress);
			return hmb;
		} else {
			return new MatchBy(simpleExpress, complexExpress);
		}
	}

	public static Algm3Row getJoinBy(String simpleExpress,
			String complexExpress, boolean append) {
		boolean canIndex = checkIndex(simpleExpress);

		if (canIndex) {
			HashJoinBy hmb = new HashJoinBy(simpleExpress, complexExpress,
					append);
			return hmb;
		} else {
			return new JoinBy(simpleExpress, complexExpress, append);
		}
	}

}
